package com.sleet.yuemiao.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.sleet.yuemiao.comment.ControllerInfoEntity;
import com.sleet.yuemiao.comment.SpringUtils;
import com.sleet.yuemiao.comment.StaticPageInitController;
import com.sleet.yuemiao.pojo.PageDto;
import com.sleet.yuemiao.util.JsonUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import javax.validation.Valid;
import javax.validation.Validator;
import javax.validation.groups.Default;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Set;

/**
 * @program: yuemiao
 * @ClassName TestController
 * @description:
 * @author: Liyongxing
 * @create: 2021-12-07 19:57
 **/
@RestController
@RequestMapping("/test")
public class TestController {
    @Resource
    Validator validator;

    @PostMapping("/post")
    public Object testMappost(@RequestBody String body) throws Exception {
        JSONObject jsonObject = JSONUtil.parseObj(body);
        Object data1 = jsonObject.get("data");
        JSONObject jsonObject1 = JSONUtil.parseObj(data1);
        String scene = jsonObject.get("scene").toString();
        String mapping = jsonObject.get("mapping").toString();

        ControllerInfoEntity infos = StaticPageInitController.getInfos(scene + ":" + mapping);
        Object target = SpringUtils.getApplicationContext().getBean(infos.getBeanName());
        Method method = target.getClass().getMethod(infos.getMethodName(), infos.getParameterTypes());
        Parameter[] parameters = method.getParameters();
        Object[] arglist = null;
        if (parameters != null && parameters.length > 0) {
            arglist = new Object[parameters.length];
            for (int i = 0; i < parameters.length; i++) {
                Parameter parameter = parameters[i];
                Valid valid = parameter.getAnnotation(Valid.class);
                if (valid != null) {
                    arglist[i] = JSONUtil.toBean(jsonObject1, parameter.getType());
                    String errMsg = validate(arglist[i], Default.class);
                    if (StringUtils.isNotEmpty(errMsg)) {
                        throw new RuntimeException(errMsg);
                    }
                }
            }

        }
        Object response = method.invoke(target, arglist);

        return response;
    }

    public <T> String validate(T object, Class<?>... groups) {
        if (groups == null) {
            groups = new Class[]{Default.class};
        }
        Set<ConstraintViolation<T>> set = validator.validate(object, groups);
        StringBuffer sb = new StringBuffer();
        if (!CollectionUtils.isEmpty(set)) {
            int i = 0;
            for (ConstraintViolation<T> c : set) {
                // 这里循环获取错误信息，可以自定义格式
                sb.append(c.getMessage());
                if (set.size() - 1 != i) {
                    sb.append(",");
                }
                i++;
            }
        }
        return sb.toString();
    }
}
